﻿using System;   
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
using Cartography.Desktop;

namespace PointOfInterest.Desktop
{
    public class Group
    {
        string name;
        string filename;
        int zoom; //nivel de zoom a partir del cual el grupo es visible
        bool visible; //boleano para determinar si el grupo es visible o no
        Bitmap icon;
        XmlDocument doc;
        List<Point> visibles;

        public Group(string file,string iconPath,bool visible) {
            doc = new XmlDocument();
            filename = file;
            visibles = new List<Point>(20);
            this.visible = visible;
            doc.Load(file);
            string aux = "";
            try
            {
                aux = iconPath + "\\" + doc.SelectSingleNode("/PointsOfInterest/Icon").InnerText;
                icon = new Bitmap(iconPath + "\\" + doc.SelectSingleNode("/PointsOfInterest/Icon").InnerText);
            }
            catch (Exception) {
                icon = new Bitmap(32, 32);
                for (int i = 0; i < 32; i++)
                    for (int j = 0; j < 32; j++)
                    {
                        if(i==j || (31-i)==j)
                            icon.SetPixel(i, j, Color.Red);
                        else
                            icon.SetPixel(i, j, Color.Transparent);
                    }
            }
            this.name = doc.SelectSingleNode("/PointsOfInterest/Name").InnerText;
            try
            {
                this.zoom = int.Parse(doc.SelectSingleNode("/PointsOfInterest/Zoom").InnerText);
            }
            catch (Exception) {
                this.zoom = 0;
            }
        }
        public Group(XmlDocument file, string iconPath, bool visible)
        {
            doc = file;
            visibles = new List<Point>(20);
            this.visible = visible;
            try
            {
                icon = new Bitmap(iconPath + "\\" + doc.SelectSingleNode("/PointsOfInterest/Icon").InnerText);
            }
            catch (Exception)
            {
                icon = new Bitmap(32, 32);
                for (int i = 0; i < 32; i++)
                    for (int j = 0; j < 32; j++)
                    {
                        if (i == j || (31 - i) == j)
                            icon.SetPixel(i, j, Color.Red);
                        else
                            icon.SetPixel(i, j, Color.Transparent);
                    }
            }
            this.name = doc.SelectSingleNode("/PointsOfInterest/Name").InnerText;
            try
            {
                this.zoom = int.Parse(doc.SelectSingleNode("/PointsOfInterest/Zoom").InnerText);
            }
            catch (Exception)
            {
                this.zoom = 0;
            }
        }
        public void update(LatLon northWest, LatLon southEast) {
            visibles.Clear();
            try
            {
                System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
                string consulta = "/PointsOfInterest/Points/Point[Coordinates/Latitude<" + northWest.Latitude.ToString(nfi) + " and Coordinates/Longitude>" + northWest.Longitude.ToString(nfi) + " and Coordinates/Latitude>" + southEast.Latitude.ToString(nfi) + " and  Coordinates/Longitude<" + southEast.Longitude.ToString(nfi) + "]";
                XmlNodeList puntosVisibles = doc.SelectNodes(consulta);
                for (int i = 0; i < puntosVisibles.Count; i++)
                {
                    string name = puntosVisibles[i].SelectSingleNode("Name") == null ? this.name : puntosVisibles[i].SelectSingleNode("Name").InnerText;
                    string address = puntosVisibles[i].SelectSingleNode("Address") == null ? "" : puntosVisibles[i].SelectSingleNode("Address").InnerText;
                    //string latitude=puntosVisibles[i].SelectSingleNode("/Coordinates/Latitude").InnerText;
                    visibles.Add(new Point(new LatLon(puntosVisibles[i].SelectSingleNode("Coordinates/Latitude").InnerText, puntosVisibles[i].SelectSingleNode("Coordinates/Longitude").InnerText), name, address, icon));
                }
            }
            catch (Exception) {}
        }
        public Bitmap Icon
        {
            get { return this.icon; }
            set { this.icon = value; }
        }
        public bool Visible
        {
            get { return this.visible; }
            set { this.visible = value; }
        }
        public int Zoom
        {
            get { return this.zoom; }
            set { this.zoom = value; }
        }
        public XmlDocument Xml
        {
            get { return this.doc; }
            set { this.doc = value; }
        }
        public List<Point> visiblePoints
        {
            get { return this.visibles; }
        }
        // Agregado este getter
        public string nombre
        {
            get { return this.name;  }
        }

        public void AddPoint(string name, string address, string latitude, string longitude, string description)
        {
            if(filename == null)
                return;
            
            XmlElement aPoint = doc.CreateElement("Point");
            //System.Windows.Forms.MessageBox.Show(root.OuterXml);
            aPoint.InnerXml = "<Name>" + name + "</Name>" +
                              "<Address>" + address + "</Address>" +
                              "<Coordinates>" +
                                  "<Latitude>" + latitude + "</Latitude>" +
                                  "<Longitude>" + longitude + "</Longitude>" +
                              "</Coordinates>" + 
                              "<Description>" + description + "</Description>";
            doc["PointsOfInterest"]["Points"].AppendChild(aPoint);
            doc.Save(filename);
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

    }
}
